//
//  MNDIntegralExchangeViewController.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/16.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDIntegralExchangeViewController.h"
#import "MNDIntegralExchangeCell.h"
#import "MNDExchangeScoreModel.h"
#import "SYRefreshControl.h"
#import "MNDLoginUserModel.h"
@interface MNDIntegralExchangeViewController ()<UITableViewDelegate,UITableViewDataSource>
//模型数组
@property (nonatomic, strong) NSMutableArray<MNDExchangeScoreModel*> *exchangeModelArray;
@property (weak, nonatomic) IBOutlet UITableView *scoreExchangeTableView;
@property (assign, nonatomic) NSInteger pageNum;

@end

@implementation MNDIntegralExchangeViewController
#pragma mark -懒加载
- (NSMutableArray<MNDExchangeScoreModel *> *)exchangeModelArray {
    if (!_exchangeModelArray) {
        _exchangeModelArray = [[NSMutableArray alloc]init];
    }
    return _exchangeModelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.scoreExchangeTableView.tableFooterView = [[UIView alloc]init];
    self.scoreExchangeTableView.estimatedRowHeight = 44;
    self.scoreExchangeTableView.rowHeight = UITableViewAutomaticDimension;
    [self loadData:YES];
    [self setupUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showExchangeRecord:) name:@"showExchangeRecord" object:nil];
}

- (void)showExchangeRecord:(NSNotification *)notification {
    [self loadData:YES];
}
#pragma mark -加载数据
- (void)loadData:(BOOL)isPullDown{
    if (self.exchangeModelArray.count <= 0) {
        [self.view beginLoading];
    }
    if (isPullDown) {
        _pageNum = 1;
    }else {
        _pageNum++;
    }
    __weak typeof(self) weakSelf = self;

    if ([MNDLoginUserModel isUserLogin]) {
        //积分及头像获取
        MNDLoginUserModel *user = [MNDLoginUserModel currentLoginUser];
        
        [APIManager sendRequestWithPath:API_EXCHANGE_LIST_SCORE parameters:[MNDSign signedParamWithDict:@{@"user_id": user.user_id, @"per_page": @(_pageNum), @"pagesize":@10}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
            
            if (responseModel.status == ResponseSuccess) {
                NSArray *exchangeScoreArray = [MNDExchangeScoreModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"case_list"]];
                if (isPullDown) {
                    [weakSelf.view configBlankPage:SYBlankPageTypeNoData hasData:exchangeScoreArray.count reloadButtonBlock:nil];
                    [weakSelf.exchangeModelArray removeAllObjects];
                    
                }
                [weakSelf.exchangeModelArray addObjectsFromArray:exchangeScoreArray];
                [SYRefreshControl configAfterLoading:weakSelf.scoreExchangeTableView hasData:exchangeScoreArray.count];
            } else {
                [SYRefreshControl configAfterLoading:weakSelf.scoreExchangeTableView hasData:YES];
                [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
            }
            [weakSelf.scoreExchangeTableView reloadData];
            
        } onFailure:^(NSError *error) {
            [SYRefreshControl configAfterLoading:weakSelf.scoreExchangeTableView hasData:YES];
            if (isPullDown) {
                SYBlankPageType blankType = [weakSelf.view blankPageTypeWithError:error];
                [weakSelf.view configBlankPage:blankType hasData:weakSelf.exchangeModelArray.count reloadButtonBlock:^(id sender) {
                    [weakSelf loadData:isPullDown];
                }];
            } else {
                [NetworkProgressHUD showHudNetworkError:error];
            }
        }];
    }
}

#pragma mark -搭建界面

- (void)setupUI {
    WeakSelf(self);
    [SYRefreshControl configGifMJRefresh:self.scoreExchangeTableView refreshType:MJRefreshSupportALl beginRefresh:YES timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
        [weakself loadData:YES];
    } loadMoreBlock:^{
        [weakself loadData:NO];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _exchangeModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDExchangeScoreModel *model = self.exchangeModelArray[indexPath.row];
    MNDIntegralExchangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MNDIntegralExchangeCell"];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.case_marks];
    cell.statusLabel.text = [NSString stringWithFormat:@"【%@】",model.case_state];
    cell.caseValueLabel.text = [NSString stringWithFormat:@"-%@",model.case_value];
    cell.caseHandleTimeLabel.text = model.case_add_time;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
